package com.example.appjam;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class LibraryListActivity extends Activity {
	Intent intent;
	String intent_area, intent_dong;
	TextView area, dong;
	ListView list;
	LibraryListData da;
	ArrayList<LibraryListData> data;
	LibraryListAdapter adapter;
	public static Database db;
	static Cursor result = null, result1 = null;
	public static Context librarylistContext;
	
	ImageView tab_home, tab_bookmark, tab_map, tab_area, tab_setting;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_library_list);
		
		tab_home = (ImageView)findViewById(R.id.tab_home);
		tab_bookmark = (ImageView)findViewById(R.id.tab_bookmark);
		tab_map = (ImageView)findViewById(R.id.tab_map);
		tab_area = (ImageView)findViewById(R.id.tab_area);
		tab_setting = (ImageView)findViewById(R.id.tab_setting);
		
		tab_home.setOnClickListener(tab_listener);
		tab_bookmark.setOnClickListener(tab_listener);
		tab_map.setOnClickListener(tab_listener);
		tab_area.setOnClickListener(tab_listener);
		tab_setting.setOnClickListener(tab_listener);

		tab_area.setImageResource(R.drawable.region_selected);
		
		db = new Database(this);
		try {
			db.createDataBase();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.openDataBase();
		
		intent = getIntent();
		intent_area = intent.getStringExtra("area_name");
		intent_dong = intent.getStringExtra("dong_name");
		
		area = (TextView)findViewById(R.id.text_area_name);
		dong = (TextView)findViewById(R.id.text_dong_name);
		area.setText(intent_area);
		dong.setText(intent_dong);
		
		setData();
		
		adapter = new LibraryListAdapter(this, data);
		list = (ListView)findViewById(R.id.library_list_listView);
		list.setAdapter(adapter);
		list.setOnItemClickListener(library_info);
		
		librarylistContext = this;
	}

	ImageView.OnClickListener tab_listener = new ImageView.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId()){
			case R.id.tab_home:
				for(int j = AreaActivity.finish_activity.size()-1; j >= 0; j--)
					AreaActivity.finish_activity.get(j).finish();
				Intent intent1 = new Intent(LibraryListActivity.this, HomeActivity.class);
				startActivity(intent1);
				finish();
				break;
				
			case R.id.tab_bookmark:	
				for(int j = AreaActivity.finish_activity.size()-1; j >= 0; j--)
					AreaActivity.finish_activity.get(j).finish();
				Intent intent2 = new Intent(LibraryListActivity.this, BookmarkActivity.class);
				startActivity(intent2);
				finish();
				break;
				
			case R.id.tab_map:
				for(int j = AreaActivity.finish_activity.size()-1; j >= 0; j--)
					AreaActivity.finish_activity.get(j).finish();
				Intent intent3 = new Intent(LibraryListActivity.this, MapViewActivity.class);
				startActivity(intent3);
				finish();
				break;
				
			case R.id.tab_area:
				break;
				
			case R.id.tab_setting:
				for(int j = AreaActivity.finish_activity.size()-1; j >= 0; j--)
					AreaActivity.finish_activity.get(j).finish();
				Intent intent5 = new Intent(LibraryListActivity.this, SettingActivity.class);
				startActivity(intent5);
				finish();
				break;
			}
		}
		
	};
	
	public void setData(){
		int bookmark_icon = R.drawable.region_favorite_icon_activated;
		data = new ArrayList<LibraryListData>();
		result = db.getLibraryList(intent_area, intent_dong);
		
		if(result.moveToFirst()){
			do{
				bookmark_icon = R.drawable.region_favorite_icon_activated;
				result1 = db.isThereBookmarkData(result.getString(0));
				if(result1.moveToFirst()){
					do{
						bookmark_icon = R.drawable.region_favorite_icon_unactivated;
					}while(result1.moveToNext());
				}
				
				da = new LibraryListData(
						result.getString(0),
						bookmark_icon
				);	
				data.add(da);
			}while(result.moveToNext());
		}	
		else
			data.add(null);
	}
	
	ListView.OnItemClickListener library_info = new ListView.OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> adapterView, View v, int position,
				long id) {
			// TODO Auto-generated method stub
			String library_name;
			library_name = data.get(position).getLibrary_name();
			
			Intent intent = new Intent(LibraryListActivity.this, LibraryInfoActivity.class);
			intent.putExtra("library_name", library_name);
			startActivity(intent);
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.library_list, menu);
		return true;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		((LibraryListActivity)(LibraryListActivity.librarylistContext)).onCreate(null);
	}

}
